package com.ccb.jyzx.expand.tiantouonline.job;

import com.ccb.jyzx.expand.tiantouonline.bean.SchedulerBatch;
import com.ccb.jyzx.expand.tiantouonline.service.SchedulerBatchService;
import com.ccb.jyzx.expand.tiantouonline.utils.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 批量作业执行
 * @projectName: extend
 * @author: jianghaolin
 * @date: 2019/11/21
 * @version: 1.0
 */
@Component
public class BatchJobExecute {

    Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SchedulerBatchService schedulerBatchService;

    @Autowired
    DynamicScheduledTask dynamicScheduledTask;

    @Autowired
    private ClassifyJob job;

    /**
     * 定时任务开关
     */
    @Value("${sleSchedul.switch}")
    private boolean batchJobSwith;

    public boolean isBatchJobSwith() {
        return batchJobSwith;
    }


    /**
     * 动态定时任务启动器 定时启动 ， 从数据库读取 批量定时任务执行时间
     * 设定 动态定时任务执行周期
     */
    @Scheduled(initialDelay = 1000,fixedDelay=30*1000)
    public void scheduledXs(){
        if(isBatchJobSwith()){
            List<SchedulerBatch> scheduleBatchConfs = schedulerBatchService.findAll();
            for(int i=0; i<scheduleBatchConfs.size(); i++){
                SchedulerBatch scheduleBatchConf = scheduleBatchConfs.get(i);
                String id = scheduleBatchConf.getId();
                String hour = scheduleBatchConf.getExeHour();
                String minture = scheduleBatchConf.getExeMin();
                if(!StringUtils.isNotBlank(hour)){
                    hour = "0";
                }
                if(!StringUtils.isNotBlank(minture)){
                    minture = "0";
                }
                String cron = "0 "+minture+" "+hour+" * * ?";
                if(id.equals(ConstantUtil.EXE_TAG_ID_AM) || id.equals(ConstantUtil.EXE_TAG_ID_PM)){
                    dynamicScheduledTask.setCron(cron,id);
                }

            }
        }

    }


    /**
     * 执行上午推送
     */
    public void scheduledAm() {
        try {
            SchedulerBatch batchAm = schedulerBatchService.findOne(ConstantUtil.EXE_TAG_ID_AM);
            executBatch(batchAm);
        }catch (Exception e){
            logger.error(" 执行任务错误："+e.getMessage());
        }finally {

        }
    }

    /**
     * 执行下午推送
     */
    public void scheduledPm() {
        try {
            SchedulerBatch batchPm = schedulerBatchService.findOne(ConstantUtil.EXE_TAG_ID_PM);
            executBatch(batchPm);
        }catch (Exception e){
            logger.error(" 执行任务错误："+e.getMessage());
        }finally {

        }
    }

    /**
     * 执行打jar包
     * @param batch
     */
    private void executBatch(SchedulerBatch batch) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmm");
        String exeHour = batch.getExeHour();
        String exeMin = batch.getExeMin();
        String day = sf.format(new Date());
        String configTime=day+exeHour+exeMin;
        String currentTime = sf2.format(new Date());
        logger.info("现在的时间：" + currentTime);
        logger.info("定时时间：" + configTime);
        if (currentTime.equals(configTime)){
            logger.info("-----已到定时推送任务执行阀值,开始执行批量任务!!!:"+Thread.currentThread().getId());
            logger.info("-----开始执行批量任务开始时间:" + new Date(System.currentTimeMillis()));
            job.send();
            logger.info("-----结束推送时间:" + new Date(System.currentTimeMillis()));
        }
    }

}
